<template>
    <div class="reg_wrap">
        <div class="reg_zhuce">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="用户名">
                    <el-input v-model.trim="form.name"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input placeholder="请输入密码" v-model.trim="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input placeholder="请输入密码" v-model.trim="form.repeate_password" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :plain="true" @click="onRegister">注册</el-button>
                    <el-button type="primary" @click="onReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { Message } from "element-ui";
// import request from "@/api/request.js";
import { req_register } from "@/api/registerLogin.js";
export default {
    data() {
        return {
            form: {
                name: "",
                password: "",
                repeate_password: "",
            },
        };
    },
    methods: {
        async onRegister() {
            // console.log("点击注册");
            console.log(this.form.name, this.form.password, this.form.repeate_password);
            //对比
            if (this.form.password === this.form.repeate_password) {
                // console.log("发送ajax请求");

                //传入参数
                // let result = await request(
                //     "post",
                //     "/register",
                //     {
                //         user_name: this.form.name,
                //         password: this.form.password,
                //     },
                //     "form"
                // );
                // console.log(result);

                //传入对象
                // let result = await request({
                //     method: "post",
                //     url: "/register",
                //     arg: {
                //         user_name: this.form.name,
                //         password: this.form.password,
                //     },
                //     headerType: "form",
                // });

                //外面再封装一层
                let result = await req_register({
                    method: "post",
                    url: "/register",
                    arg: {
                        user_name: this.form.name,
                        password: this.form.password,
                    },
                    headerType: "form",
                });
                console.log(result);
                //在注册成功状态下 注册之后跳转到登录页面 使用编程式页面跳转
                this.$router.push({
                    name: "login",
                });
            } else {
                Message({
                    type: "warning",
                    message: "请重新输入密码",
                });
                // 清空input框
                this.form.password = "";
                this.form.repeate_password = "";
            }
        },
        onReset() {
            console.log("重置");
            this.form.name = "";
            this.form.password = "";
            this.form.repeate_password = "";
        },
    },
};
</script>

<style lang="scss" scoped>
.reg_wrap {
    background-image: url("~@a/img/bggrey.jpg");
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .reg_zhuce {
        background-image: url("~@a/img/bggrey.jpg");
        background-size: 100% 100%;
        width: 500px;
        height: 500px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}
</style>